﻿using System;
using System.Collections.Generic;
using System.Text;
//using System.Data.Sql;
using System.Data.SqlClient;

using System.Configuration;

namespace AbstractSql {
    public class AbsConnection : IDisposable {
        #region IDisposable Members

        public void Dispose() {
            throw new NotImplementedException();
        }

        #endregion
    }
    public static class AbsDBConnection {
        #region Fields & Properties
        static Dictionary<string, string> connectionStrings = new Dictionary<string, string>();
        static string defaultConnectionString;
        static int defaultTimeOut;
        
        public static int DefaultTimeOut { get { return defaultTimeOut; } }
        #endregion
        #region Initializers
        public static void SetDefaultConnectionString(string connStringName) {
            if (ConfigurationManager.ConnectionStrings[connStringName] == null)
                throw new ArgumentException(string.Format("'{0}' not found on configuration", connStringName));
            defaultConnectionString = ConfigurationManager.ConnectionStrings[connStringName].ConnectionString;
        }
        public static void SetDefaultConnectionStringFromString(string connectionString) {
            defaultConnectionString = connectionString;
        }
        public static void SetDefaultTimeOut(int timeOut) {
            defaultTimeOut = timeOut;
        }
        public static void AddNamedConnectionString(string connStringName) {
            if (ConfigurationManager.ConnectionStrings[connStringName] == null)
                throw new ArgumentException(string.Format("'{0}' not found on configuration", connStringName));

            string connStringValue = ConfigurationManager.ConnectionStrings[connStringName].ConnectionString;
            connectionStrings.Add(connStringName, connStringValue);
        }
        #endregion
        //*** return connections
        internal static SqlConnection GetNamedConnection(string name) {
            if (!connectionStrings.ContainsKey(name))
                throw new Exception(string.Format("named connection [{0}] has not been registered", name));
            return new SqlConnection(connectionStrings[name]);
        }
        internal static SqlConnection GetDefaultConnection() {
            return new SqlConnection(defaultConnectionString);
        }
        static AbsConnection GetDBConnection() {
            return new AbsConnection();
        }
        public static IAbsDBCommand GetAbsDBCommandFor(string connectionName) {
            return AbsDBCommand.WithConnection(connectionName);
        }
        public static IAbsDBCommand GetAbsDBCommandForDefault() {
            return new _AbsDBCommand();
        }
    }
}